package com.onlyxiahui.common.action.description.util;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.lang.reflect.Type;
import java.util.List;

import com.thoughtworks.qdox.model.JavaMethod;
import com.thoughtworks.qdox.model.JavaParameter;

/**
 * Description <br>
 * Date 2020-01-08 14:38:09<br>
 * 
 * @author XiaHui [onlovexiahui@qq.com]<br>
 * @since 1.0.0
 */

public class ActionKeyUtil {

	public static String getKey(JavaMethod jm) {
		if (null != jm) {
			String name = jm.getName();
			StringBuilder sb = new StringBuilder(name);
			List<JavaParameter> parameters = jm.getParameters();
			String key = ActionKeyUtil.getKey(parameters);
			if (null != key) {
				sb.append("-");
				sb.append(key);
			}
			return sb.toString();
		}
		return null;
	}

	public static String getKey(List<JavaParameter> parameters) {
		if (null != parameters && !parameters.isEmpty()) {
			StringBuilder sb = new StringBuilder();
			int size = parameters.size();
			int end = size - 1;
			for (int i = 0; i < size; i++) {
				JavaParameter jp = parameters.get(i);
				// System.out.println();
				// System.out.println(JSONObject.toJSONString(jp.getType()));
				sb.append(jp.getType().getFullyQualifiedName());
				if (i < end) {
					sb.append("-");
				}
			}
			return sb.toString();
		}
		return null;
	}

	public static String getKey(Method m) {
		if (null != m) {
			String name = m.getName();
			Parameter[] parameters = m.getParameters();
			return getKey(name, parameters);
		}
		return null;
	}

	public static String getKey(String name, Parameter[] parameters) {
		StringBuilder sb = new StringBuilder(name);
		String key = ActionKeyUtil.getKey(parameters);
		if (null != key) {
			sb.append("-");
			sb.append(key);
		}
		return sb.toString();
	}

	public static String getKey(Parameter[] parameters) {
		if (null != parameters) {
			int size = parameters.length;
			if (size > 0) {
				StringBuilder sb = new StringBuilder();
				int end = size - 1;
				for (int i = 0; i < size; i++) {
					Parameter jp = parameters[i];

					Class<?> calssType = jp.getType();
					final Type type = jp.getParameterizedType();
					final String typename = type.getTypeName();

					if (jp.isVarArgs()) {
						sb.append(typename.replaceFirst("\\[\\]$", "..."));
					} else {

						if (calssType.isArray()) {
							sb.append(typename);
						} else {
							sb.append(calssType.getName());
						}

//						int startIndex = typename.indexOf("<");
//						int lastIndex = typename.indexOf(">");
//						int endIndex = lastIndex + 1;
//						int length = typename.length();
//						if (startIndex != -1 && endIndex != -1 && endIndex < length) {
//							sb.append(typename.substring(0, startIndex));
//							sb.append(typename.substring(endIndex, length));
//						} else {
//							sb.append(typename);
//						}

//						if (calssType.isArray()) {
//							if (type instanceof ParameterizedType) {
//
//							} else {
//								sb.append(typename);
//							}
//						} else {
//							sb.append(calssType.getName());
//						}
					}
					if (i < end) {
						sb.append("-");
					}
				}
				return sb.toString();
			}
		}
		return null;
	}
}
